import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {CommProvider} from "../../providers/CommProvider";
import {HttpService} from "../../providers/HttpService";

/**
 * Generated class for the PasswordPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-password',
  templateUrl: 'password.html',
})
export class PasswordPage {

    reset: {phone: string, code: string, password: string} = {
        phone: null,
        code: null,
        password: null
    };

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private commProvider: CommProvider,
                private httpService: HttpService) {
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad PasswordPage');
    }

    onSendCode() {
        this.httpService.instance()
            .setUrl('/user/sendSmsCode')
            .setBody({
                type: 'resetPassword',
                phone: this.reset.phone
            })
            .postRequest()
            .then(res => {
                if (res.status == 0) {
                    this.commProvider.showToast('验证码已发送');
                }
            });
    }

    onPasswordReset() {
        this.httpService.instance()
            .setUrl('/user/userResetPassword')
            .setBody(this.reset)
            .putRequest()
            .then(res => {
                if (res.status == 0) {
                    this.commProvider.showToast('密码重置成功');
                    this.navCtrl.pop();
                }
            });
    }
}
